package com.lnsf.bookstore.common.shiro.realm;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.HostAuthenticationToken;
import org.apache.shiro.authc.RememberMeAuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import com.lnsf.bookstore.system.user.service.UserService;
import com.lnsf.bookstore.system.user.vo.User;

public class UPToken extends AuthorizingRealm implements HostAuthenticationToken, RememberMeAuthenticationToken {


	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String phone;
	
	private String password;
	
	private boolean rememberMe = false;
	
	private String host;
	
	@Autowired
	private UserService userService;
	
	
	 public UPToken(final String phone, final String password) {
		 	this.phone = phone;
	        this.password = password;
	 }
	 
	 
	
	public UPToken(String phone, String password, boolean rememberMe, String host) {
		super();
		this.phone = phone;
		this.password = password;
		this.rememberMe = rememberMe;
		this.host = host;
	}



	public UPToken() {
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Object getPrincipal() {
		return getPhone();
	}

	public Object getCredentials() {
		return getPassword();
	}

	public void setRememberMe(boolean rememberMe) {
		this.rememberMe = rememberMe;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public boolean isRememberMe() {
		// TODO Auto-generated method stub
		return rememberMe;
	}

	public String getHost() {
		// TODO Auto-generated method stub
		return host;
	}



	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		// TODO Auto-generated method stub
		return null;
	}



	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		//String username= (String) token.getPrincipal();
				String phone= (String) token.getPrincipal();
				//System.out.println(username);
		        //VipUser vipuser = userService.getByUserName(username);
				User user = userService.getByPhone(phone);
				
		        if(user == null) {  
		            throw new UnknownAccountException();//没找到帐号  
		        } 
		        //交给AuthenticatingRealm使用CredentialsMatcher进行密码匹配，如果觉得人家的不好可以在此判断或自定义实现  
		        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(  
		                user.getUsername(), //用户名  
		                user.getPassword(), //密码  
		                ByteSource.Util.bytes(user.getSalt()), 
		                getName()  //realm name  
		        );  
		        
		        
		       System.out.println(user.getSalt());
		        return authenticationInfo; 
	}


	
	
	
	
	
	
}
